package com.example.wheelguide;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddressActivity extends Activity {
	Address address;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.address);
	
		Intent i = getIntent();
		address = (Address)i.getSerializableExtra("address");
		System.out.println("Street:" + address.getStreet_());
		
		Button button1 = new Button(this);
		
		
		final EditText et = (EditText) findViewById(R.id.editText_street);
		et.setText(address.getStreet_());
		final EditText et2 = (EditText) findViewById(R.id.EditText_street_number);
		et2.setText(address.getStreet_number_());
		final EditText et3 = (EditText) findViewById(R.id.EditText_zipcode);
		et3.setText(address.getZipcode_());
		final EditText et4 = (EditText) findViewById(R.id.EditText_place);
		et4.setText(address.getPlace_());
		
		
		button1 = (Button) findViewById(R.id.Button_apply);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				if((et.getText().length()==0)||(et2.getText().length()==0)||(et3.getText().length()==0)||(et4.getText().length()==0))
				{
					
					AlertDialog.Builder alertDialogBuilder= new AlertDialog.Builder(v.getContext());
					alertDialogBuilder.setTitle("!!!Angaben fehlen!!!");
					alertDialogBuilder.setMessage("Bitte tragen Sie die Angaben nach.");
					
					alertDialogBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							dialog.cancel();
						}
					  });
					
					AlertDialog ad = alertDialogBuilder.create();
					ad.show();
				}
				else
				{
					
					address.setStreet_(et.getText().toString());
					address.setStreet_number_(et2.getText().toString());
					address.setZipcode_(et3.getText().toString());
					address.setPlace_(et4.getText().toString());
					
					
					Intent i = new Intent();
					Bundle b= new Bundle();
					b.putSerializable("return_address", address);
					i.putExtras(b);
					setResult(100, i);
					finish();
				}
			}
		});
		
		button1 = (Button) findViewById(R.id.button_back);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}
